/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.gwt.module;

public class BaseApplication {
	static protected void processException(Presentation p, Throwable caught) {
		
		String msg = caught.getMessage();

		if(msg.length() > 1 && msg.substring(0, 2).equals("__")) {
			
			/*
			 * We must make sure that we cover the case where the string ends in __
			 * or not, since GWT's javascript version of String.split() works different
			 * than java's String.split() for empty tokens.
			 */
			String [] tokens;
			if(msg.substring(msg.length()-2, msg.length()).equals("__"))
				tokens = msg.substring(2,msg.length()-2).split("__");
			else
				tokens = msg.substring(2,msg.length()).split("__");
			
			String type = tokens[0];
			
			if(type.equals("validation")) {
				
				//there most be an even number of tokens (key:value pairs)
				if((tokens.length - 1) % 2 == 0) {
					
					for(int i = 1; i < tokens.length; i += 2) {
						String key = tokens[i];
						String value = tokens[i+1];
						p.displayError(key,value);
					}
				}
			} else {
				p.displayStatus(caught.getMessage(), 10);
				System.out.println(":::" + caught.getMessage());
				caught.printStackTrace();
			}
		} else {
			p.displayStatus(caught.getMessage(), 10);			
			System.out.println(":::" + caught.getMessage());
			caught.printStackTrace();
		}
		
		
	}
}
